package com.specificgroup.jira.plugin.acpro.action;

import com.specificgroup.jira.plugin.acpro.Constants;
import com.specificgroup.jira.plugin.acpro.exception.AcproException;
import com.specificgroup.jira.plugin.acpro.model.Settings;
import com.specificgroup.jira.plugin.acpro.service.JiraAcproSettingsService;
import com.specificgroup.jira.plugin.acpro.session.SessionTempData;
import com.specificgroup.jira.plugin.acpro.session.SessionUtil;
import com.specificgroup.jira.plugin.acpro.xml.settings.generated.JiraAcproSettingsType;
import org.apache.log4j.Logger;
import webwork.action.ServletActionContext;
import webwork.multipart.MultiPartRequestWrapper;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author Ivan Holub
 */
public class LoadSettingsAction extends AcproActionSupport {

    private static final String NEXT_ACTION = "AcproLogin!default.jspa";

    private JiraAcproSettingsService jiraAcproSettingsService;

    private Logger log = Logger.getLogger(getClass());

    public LoadSettingsAction(JiraAcproSettingsService jiraAcproSettingsService) {
        this.jiraAcproSettingsService = jiraAcproSettingsService;
    }

    @Override
    public String doDefault() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }

        HttpSession session = ServletActionContext.getRequest().getSession();
        Settings settings = new Settings();
        session.setAttribute(Constants.SETTINGS_SESSION_KEY, settings);

        SessionTempData sessionTempData = new SessionTempData();
        session.setAttribute(Constants.SESSION_TEMP_DATA_KEY, sessionTempData);

        return super.doDefault();
    }

    @Override
    protected String doExecute() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }

        MultiPartRequestWrapper wrapper = ServletActionContext.getMultiPartRequest();

        Enumeration files = wrapper.getFileNames();
        if (files.hasMoreElements()) {
            String fileName = (String) files.nextElement();
            File file = wrapper.getFile(fileName);

            try {
                JiraAcproSettingsType xmlSettings = jiraAcproSettingsService.unmarshalResponse(file);
                Settings settings = SessionUtil.getSettings();
                settings.setLoadedSettings(xmlSettings);

            } catch (AcproException e) {
                log.error("Error", e);
                addErrorMessage(getText("error.upload") + " " + e.getMessage());
                return ERROR;
            } catch (IOException e) {
                log.error("Error", e);
                addErrorMessage(getText("error.upload") + " " + e.getMessage());
                return ERROR;
            }
        }

        String redirect = getRedirect(NEXT_ACTION);
        return redirect;
    }

}
